#include "GrayValueMeasurementDrawerFactory.h"
#include "HorizontalSingleLineDrawer.h"
#include "VerticalSingleLineDrawer.h"
#include "RectangleRegionDrawer.h"
#include "MeasurementAlgorithmBase.h"
#include <QDebug>

GrayValueMeasurementDrawerFactory::GrayValueMeasurementDrawerFactory(QObject *parent) 
    : QObject(parent),
      m_scene(nullptr)
{
    // 初始化各种绘制器
    m_horizontalSingleLineDrawer = std::make_unique<HorizontalSingleLineDrawer>(this);
    m_verticalSingleLineDrawer = std::make_unique<VerticalSingleLineDrawer>(this);
    m_rectangleRegionDrawer = std::make_unique<RectangleRegionDrawer>(this);
    
    // 连接绘制器信号到工厂类信号
    connect(m_rectangleRegionDrawer.get(), &RectangleRegionDrawer::rectangleChanged, 
            this, &GrayValueMeasurementDrawerFactory::rectangleChanged);
    
    // 假设HorizontalSingleLineDrawer和VerticalSingleLineDrawer也有类似的信号
    connect(m_horizontalSingleLineDrawer.get(), &HorizontalSingleLineDrawer::lineChanged,
            this, &GrayValueMeasurementDrawerFactory::horizontalLineChanged);
    connect(m_verticalSingleLineDrawer.get(), &VerticalSingleLineDrawer::lineChanged,
            this, &GrayValueMeasurementDrawerFactory::horizontalLineChanged);
}

GrayValueMeasurementDrawerFactory::~GrayValueMeasurementDrawerFactory()
{
    // 自动释放资源
}

void GrayValueMeasurementDrawerFactory::setScene(QGraphicsScene *scene)
{
    if (m_scene != scene) {
        m_scene = scene;
        
        // 将场景设置给所有绘制器
        m_horizontalSingleLineDrawer->setScene(scene);
        m_verticalSingleLineDrawer->setScene(scene);
        m_rectangleRegionDrawer->setScene(scene);
    }
}

QObject *GrayValueMeasurementDrawerFactory::getDrawer(MeasurementMode mode)
{
    switch (mode) {
    case MeasurementMode::HorizontalSingleLine:
        return m_horizontalSingleLineDrawer.get();
    case MeasurementMode::VerticalSingleLine:
        return m_verticalSingleLineDrawer.get();
    case MeasurementMode::RectangleRegion:
        return m_rectangleRegionDrawer.get();
    default:
        qWarning() << "未知的测量模式:" << static_cast<int>(mode);
        return nullptr;
    }
}

void GrayValueMeasurementDrawerFactory::initializeMeasurementTool(MeasurementMode mode, int imageWidth, 
                                                                int imageHeight, double scale, 
                                                                const QPointF &offset)
{
    // 首先清除所有绘制内容
    clearAll();
    
    // 根据模式初始化对应的测量工具
    switch (mode) {
    case MeasurementMode::HorizontalSingleLine:
        m_horizontalSingleLineDrawer->initializeLine(imageWidth, imageHeight, scale, offset);
        break;
    case MeasurementMode::VerticalSingleLine:
        m_verticalSingleLineDrawer->initializeLine(imageWidth, imageHeight, scale, offset);
        break;
    case MeasurementMode::RectangleRegion:
        m_rectangleRegionDrawer->initializeRectangle(imageWidth, imageHeight, scale, offset);
        break;
    default:
        qWarning() << "无法初始化未知的测量模式:" << static_cast<int>(mode);
        break;
    }
}

void GrayValueMeasurementDrawerFactory::restoreFromResult(MeasurementMode mode, const QVariantMap &result, 
                                                        double scale, const QPointF &offset)
{
    // 首先清除所有绘制内容
    clearAll();
    
    // 根据模式从结果中恢复对应的测量工具
    switch (mode) {
    case MeasurementMode::HorizontalSingleLine:
        m_horizontalSingleLineDrawer->restoreFromResult(result, scale, offset);
        break;
    case MeasurementMode::VerticalSingleLine:
        m_verticalSingleLineDrawer->restoreFromResult(result, scale, offset);
        break;
    case MeasurementMode::RectangleRegion:
        m_rectangleRegionDrawer->restoreFromResult(result, scale, offset);
        break;
    default:
        qWarning() << "无法恢复未知的测量模式:" << static_cast<int>(mode);
        break;
    }
}

void GrayValueMeasurementDrawerFactory::updateTransformInfo(MeasurementMode mode, double scale, 
                                                          const QPointF &offset)
{
    // 根据模式更新对应的测量工具的变换信息
    switch (mode) {
    case MeasurementMode::HorizontalSingleLine:
        m_horizontalSingleLineDrawer->updateTransformInfo(scale, offset);
        break;
    case MeasurementMode::VerticalSingleLine:
        m_verticalSingleLineDrawer->updateTransformInfo(scale, offset);
        break;
    case MeasurementMode::RectangleRegion:
        m_rectangleRegionDrawer->updateTransformInfo(scale, offset);
        break;
    default:
        qWarning() << "无法更新未知的测量模式:" << static_cast<int>(mode);
        break;
    }
}

void GrayValueMeasurementDrawerFactory::setParameters(const QVariantMap &params)
{
    // 将参数设置给所有绘制器
    m_horizontalSingleLineDrawer->setParameters(params);
    m_verticalSingleLineDrawer->setParameters(params);
    m_rectangleRegionDrawer->setParameters(params);
}

void GrayValueMeasurementDrawerFactory::clearAll()
{
    // 清除所有绘制器的内容
    m_horizontalSingleLineDrawer->clearAll();
    m_verticalSingleLineDrawer->clearAll();
    m_rectangleRegionDrawer->clearAll();
}

void GrayValueMeasurementDrawerFactory::updateScale(double scale)
{
    // 更新所有绘制器的缩放比例
    m_horizontalSingleLineDrawer->setScale(scale);
    m_verticalSingleLineDrawer->setScale(scale);
    m_rectangleRegionDrawer->setScale(scale);
}
